package com.sqx.modules.tbCoupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.tbCoupon.dao.TbCouponDao;
import com.sqx.modules.tbCoupon.entity.TbCoupon;
import com.sqx.modules.tbCoupon.entity.TbCouponUser;
import com.sqx.modules.tbCoupon.service.TbCouponService;
import com.sqx.modules.tbCoupon.service.TbCouponUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author www.javacoder.top
 * @since 2022-11-18
 */
@Service
public class TbCouponServiceImpl extends ServiceImpl<TbCouponDao, TbCoupon> implements TbCouponService {

    @Autowired
    private TbCouponDao tbCouponDao;
    @Autowired
    private UserService userService;
    @Autowired
    private TbCouponUserService couponUserService;

    @Override
    public Result addCoupon(TbCoupon tbCoupon) {
        tbCoupon.setDeleteFlag(0);
        tbCouponDao.insert(tbCoupon);
        return Result.success();
    }

    @Override
    public IPage<TbCoupon> getCouponPageList(Integer page, Integer limit, TbCoupon tbCoupon) {
        Page<TbCoupon> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return tbCouponDao.selectPage(pages, new QueryWrapper<>(tbCoupon));
    }

    @Override
    public Result deleteCoupon(Long couponId) {
        return Result.upStatus(tbCouponDao.deleteById(couponId));
    }

    @Override
    public Result updateCoupon(TbCoupon tbCoupon) {
        return Result.upStatus(tbCouponDao.updateById(tbCoupon));
    }

    @Transactional
    @Override
    public Result giveCoupon(Long couponId, List<String> userIdList) {
        TbCoupon tbCoupon = tbCouponDao.selectById(couponId);
        for (String userId : userIdList) {
            TbCouponUser tbCouponUser = new TbCouponUser();
            for (int i = 0; i < tbCoupon.getCouponNum(); i++) {
                BeanUtils.copyProperties(tbCoupon, tbCouponUser);
                tbCouponUser.setCreateTime(new Date());
                if (tbCoupon.getValidDays() != null && tbCoupon.getValidDays() != 0) {
                    LocalDateTime dateTime = LocalDateTime.now().plusDays(tbCoupon.getValidDays());
                    ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
                    Instant instant = zonedDateTime.toInstant();
                    tbCouponUser.setExpirationTime(Date.from(instant));
                }
                tbCouponUser.setUserId(Long.valueOf(userId));
                tbCouponUser.setStatus(0);
                couponUserService.save(tbCouponUser);
            }
        }

        return Result.success();
    }


}
